package com.lx.zy.project.system.service.impl;

import com.lx.zy.common.utils.StringUtils;
import com.lx.zy.project.system.domain.SysRole;
import com.lx.zy.project.system.mapper.SysRoleMapper;
import com.lx.zy.project.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @program: zy-vue
 * @description: 角色 业务层实现
 * @author: Lx
 * @create: 2019-10-23 11:21
 **/
@Service
public class SysRoleServiceImpl implements ISysRoleService
{

    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public Set<String> selectRolePermissionByUserId(Long userId)
    {
        List<SysRole> perms = roleMapper.selectRolePermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (SysRole perm : perms)
        {
            if (StringUtils.isNull(perm))
            {
                permsSet.addAll(Arrays.asList(perm.getRoleKey()));
            }
        }
        return permsSet;
    }

    @Override
    public List<SysRole> selectRoleList(SysRole role)
    {
        return roleMapper.selectRoleList(role);
    }

    /**
     * 根据用户ID获取角色选择框列表
     *
     * @param userId 用户ID
     * @return 选中角色ID列表
     */
    @Override
    public List<Integer> selectRoleListByUserId(Long userId)
    {
        return roleMapper.selectRoleListByUserId(userId);
    }
}
